<!-- 新增编辑车辆 -->
<template>
  <div>
    <el-dialog
      title="新增车辆"
      width="600px"
      :visible="dialogIsShow"
      :close-on-click-modal="false"
      @close="closeDialog"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="form"
        label-position="right"
        label-width="110px"
        style="width: 100%"
      >

        <el-form-item
          label="车辆类型："
          prop="truckTypeId"
        >
          <el-select
            v-model="form.truckTypeId"
            placeholder="请选择"
            style="width: 100%"
            clearable
            value-key="id"
          >
            <el-option
              v-for="item in carTypeList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="车牌号码："
          prop="licensePlate"
        >
          <el-input
            v-model="form.licensePlate"
            placeholder="请输入车牌号码"
            maxlength="8"
          />
        </el-form-item>
        <el-form-item
          label="GPS设备ID："
          prop="deviceGpsId"
        >
          <el-input
            v-model="form.deviceGpsId"
            placeholder="请输入GPS设备ID"
            maxlength="15"
          />
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
        style="text-align: center"
      >
        <el-button
          class="save-btn"
          @click="submitForm"
        >确认</el-button>
        <el-button
          class="cancel-btn"
          @click="closeDialog"
        >取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { truckTypeAdd } from '@/api/transit'
export default {
  name: 'VehicleAdd',
  props: {
    carTypeList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      form: {
        deviceGpsId: '',
        licensePlate: '',
        truckTypeId: ''
      },
      rules: {
        truckTypeId: [
          { required: true, message: '车辆类型不能为空', trigger: 'change' }
        ],
        licensePlate: [
          { required: true, message: '车牌号码不能为空', trigger: 'blur' },
          { pattern: /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/, message: '请输入正确的车牌号码', trigger: 'blur' }
        ],
        deviceGpsId: [
          { required: true, message: 'GPS设备ID不能为空', trigger: 'blur' }
        ]
      },
      dialogIsShow: false
    }
  },
  created() {

  },
  methods: {
    validate() {
      return
    },
    // 打开弹窗
    openDialog() {
      this.dialogIsShow = true
    },
    // 关闭弹窗
    closeDialog() {
      this.dialogIsShow = false
      this.$refs.dataForm.resetFields()
    },
    // 提交添加数据
    submitForm() {
      this.$refs.dataForm.validate(async valid => {
        if (!valid) return
        await truckTypeAdd(this.form)
        this.$message.success('添加成功')
        this.closeDialog()
        this.$emit('submitForm')
      })
    }
  }
}
</script>
<style scoped lang="scss">
/deep/ .el-dialog__body {
  padding: 40px 50px 0 20px !important;
}
</style>
